const { default: mongoose } = require("mongoose");

const userSchema = new mongoose.Schema(
  {
    username: {
      type: String,
      required: true,
      unique: true,
      min: 3,
      max: 20,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
    password: {
      type: String,
    },
    img: {
      type: String,
    },
    isAdmin: {
      type: Boolean,
      default: false,
    },
  },
  { timestamps: true }
);

const postSchema = mongoose.Schema(
  {
    title: { type: String, required: true, unique: true },
    body: { type: String, required: true },
    img: { type: String },
    // user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
    userId: { type: String, required: true },
    // slug: { type: String, required: true, unique: true },
  },
  { timestamps: true }
);

// 建表， 集合默认是 小写 复数  users   posts
export const User = mongoose.models.User || mongoose.model("User", userSchema);
export const Post = mongoose.models.Post || mongoose.model("Post", postSchema);
